1
|
|
|
/* |
2
|
|
|
* @Author: jdi-juma |
3
|
|
|
* @Date: 2017-12-09 17:11:58 |
4
|
|
|
* @Last Modified by: jdi-juma |
5
|
|
|
* @Last Modified time: 2017-12-09 22:07:37 |
6
|
|
|
*/ |
7
|
|
|
// NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23 |
8
|
|
|
var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection; |
9
|
|
|
|
10
|
|
|
if (RTCPeerConnection) (function () { |
|
|
|
|
11
|
|
|
var rtc = new RTCPeerConnection({iceServers:[]}); |
12
|
|
|
if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed |
13
|
|
|
rtc.createDataChannel('', {reliable:false}); |
14
|
|
|
}; |
15
|
|
|
|
16
|
|
|
rtc.onicecandidate = function (evt) { |
17
|
|
|
// convert the candidate to SDP so we can run it through our general parser |
18
|
|
|
// see https://twitter.com/lancestout/status/525796175425720320 for details |
19
|
|
|
if (evt.candidate) grepSDP("a="+evt.candidate.candidate); |
|
|
|
|
20
|
|
|
}; |
21
|
|
|
rtc.createOffer(function (offerDesc) { |
22
|
|
|
grepSDP(offerDesc.sdp); |
23
|
|
|
rtc.setLocalDescription(offerDesc); |
24
|
|
|
}, function (e) { console.warn("offer failed", e); }); |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
var addrs = Object.create(null); |
28
|
|
|
addrs["0.0.0.0"] = false; |
29
|
|
|
function updateDisplay(newAddr) { |
30
|
|
|
if (newAddr in addrs) return; |
|
|
|
|
31
|
|
|
else addrs[newAddr] = true; |
32
|
|
|
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; }); |
33
|
|
|
//document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a"; |
34
|
|
|
document.getElementById('list').value = displayAddrs; |
35
|
|
|
if($("meta[name=ipaddress]").attr('content') == '') |
36
|
|
|
{ |
37
|
|
|
$("meta[name=ipaddress]").attr('content', displayAddrs); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function grepSDP(sdp) { |
43
|
|
|
var hosts = []; |
|
|
|
|
44
|
|
|
sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39 |
45
|
|
|
if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13 |
46
|
|
|
var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1 |
47
|
|
|
addr = parts[4], |
48
|
|
|
type = parts[7]; |
49
|
|
|
if (type === 'host') updateDisplay(addr); |
|
|
|
|
50
|
|
|
} else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7 |
51
|
|
|
var parts = line.split(' '), |
|
|
|
|
52
|
|
|
addr = parts[2]; |
|
|
|
|
53
|
|
|
updateDisplay(addr); |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
})(); else { |
58
|
|
|
|
59
|
|
|
document.getElementById('list').value = "<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>"; |
60
|
|
|
//$('#list').val("<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>"); |
61
|
|
|
//document.getElementById('list').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull."; |
62
|
|
|
} |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.